﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using ServerApi.Models;
using ServerApi.Models.Menu;
using TuyennvLibrary.Utl;
namespace ServerApi.Controllers
{
    public class AdminController
    {
        static AccountModel accountmodel = new AccountModel();
        static RoleUserModel roleUserModel = new RoleUserModel();
        public static string SESSION_USERCURRENT = "usercurrent";
        public static bool checkLogin(string username, string password)
        {
            if (accountmodel.checklogin(username, password).Id > 0)
                return true;
            return false;
        }
        public static Account getAccount(string username, string password)
        {
            return accountmodel.getAccount(username, password);
        }
        public static bool checkLogin(HttpSessionState Session)
        {
            try
            {
                string username = (Session["username"] == null) ? string.Empty : Session["username"].ToString();
                string password = (Session["password"] == null) ? string.Empty : Session["password"].ToString();

                if (checkLogin(username, password))
                {
                    return true;
                }

            }
            catch (Exception ex)
            {
            }
            return false;
        }
        public static void checkLogin(HttpSessionState Session, HttpRequest Request, HttpResponse Response)
        {
            string url = Request.Url.AbsolutePath;
            string query = Request.Url.Query;
            string page = url.Substring(url.LastIndexOf('/') + 1);

            if (!string.IsNullOrEmpty(page) && page != "Default.aspx")
            {
                try
                {
                    string username = (Session["username"] == null) ? string.Empty : Session["username"].ToString();
                    string password = (Session["password"] == null) ? string.Empty : Session["password"].ToString();

                    if (!checkLogin(username, password))
                    {
                        Response.Redirect("/administrator/Default.aspx?page=" + Request.Url.AbsolutePath + query);
                    }

                }
                catch (Exception ex)
                {
                    Response.Redirect("/administrator/Default.aspx?page=" + Request.Url.AbsolutePath + query);
                }
            }
        }
        public static void checkPermission(Page PageClient, System.Type type, HttpSessionState Session, HttpRequest Request, HttpResponse Response)
        {
            string url = Request.Url.AbsolutePath;
            string page = url.Substring(url.IndexOf('/', 0));
            int userid = (int)Session["userid"];
            if (!roleUserModel.haveRole(userid, page))
            {
                Utilities.Script(PageClient, "thong bao", string.Format("<script>alert('Ban khong co quyen vao trang nay');window.location='Default.aspx'</script>"));

                //Response.Redirect("ErrorPermission.aspx?page=" + page);
            }
        }
        public static List<Role> getListRoleBy(List<Role> source, int theloai)
        {
            var l = source.Where(p => p.TypeRoleId == theloai).ToList<Role>();
            return l;
        }
        //public static List<int> getListMenu(List<role> source)
        //{
        //    var l = source.Select(p => p.weight).Distinct().ToList<int>();
        //    return l;
        //}
        public static List<TypeRole> getListMenuCatgories(int userid)
        {
            return new TypeRoleModel().getListMenu(userid);
        }
        //public static List<MenuCategory> getListMenuCatgories()
        //{
        //    List<MenuCategory> list = new List<MenuCategory>();
        //    list.Add(new MenuCategory { id = 1, Name = "Quản lý Tài khoản" });
        //    list.Add(new MenuCategory { id = 2, Name = "Quản lý Ứng dụng" });
        //    list.Add(new MenuCategory { id = 3, Name = "Quản lý GCM" });
        //    list.Add(new MenuCategory { id = 4, Name = "Quản lý SMS" });
        //    list.Add(new MenuCategory { id = 5, Name = "Quản lý Quảng cáo" });
        //    list.Add(new MenuCategory { id = 6, Name = "Quản lý Lỗi" });
        //    return list;
        //}
    }
}